//index.js
//获取应用实例
const app = getApp()
// console.log(app.globalData.status)
var util = require('../../utils/util.js');
Page({
  data: {
    interal: '',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    time: "",
    status: "",
    msg: "",
    hasScan: false,
    bill_num: null
  },

  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function() {
    var that = this;
    // 调用函数时，传入new Date()参数，返回值是日期和时间
    var time = util.formatTime(new Date());
    // 再通过setData更改Page()里面的data，动态更新页面的数据
    this.setData({
      time: time,
    })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true,
        interal: app.globalData.interal,
        status: app.globalData.status,
        msg: app.globalData.msg
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true,
          interal: app.globalData.interal,
          status: app.globalData.status,
          msg: app.globalData.msg
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true,
            interal: app.globalData.interal,
            status: app.globalData.status,
            msg: app.globalData.msg
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    var that = this;
    console.log(app.globalData.address, that.data.bill_num)
    app.globalData.userInfo = e.detail.userInfo
    if (e.detail.userInfo) {
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true,
        interal: app.globalData.interal,
        status: app.globalData.status,
        msg: app.globalData.msg
      })
      wx.login({
        success: res => {
          // that.globalData.res_code = res.code
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          console.log(res.code)
          wx.request({
            url: 'https://api.tanghaolun.cn/xxd_reg',
            method: 'POST',
            data: {
              "code": res.code,
              "address": app.globalData.address,
              "bill_num": that.data.bill_num
            },
            success: function(result) {
              console.log(result)
              if(result.data.status==305 && app.globalData.status == 200){
                that.setData({
                  status: result.data.status,
                  msg: result.data.msg
                })
                wx.login({
                  success: res => {
                    // that.globalData.res_code = res.code
                    // 发送 res.code 到后台换取 openId, sessionKey, unionId
                    console.log(res.code)
                    wx.request({
                      url: 'https://api.tanghaolun.cn/delete',
                      method: 'POST',
                      data: {
                        "code": res.code,
                      },
                      success: function (result) {
                        console.log(result)
                      },
                      fail: function () {
                        console.log("ajax error")
                      }
                    })
                  }
                })
              }
            },
            fail: function() {
              console.log("ajax error")
            }
          })
        }
      })
    } else {
      console.log("用户拒绝授权")
    }
  },
  test: function() {
    var that = this
  },
  scanCode: function() {
    var that = this;
    wx.scanCode({ //扫描API
      success(res) { //扫描成功
        console.log(res) //输出回调信息
        that.setData({
          hasScan: true,
          bill_num: res.result
        })
        wx.showToast({
          title: '成功',
          duration: 1000
        })
      }
    })
  },
  // getCityNameOFLocation: function() {
  //   var that = this;
  //   wx.getLocation({
  //     type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
  //     success: function(res) {
  //       var long = res.longitude + 0.015658,
  //         lat = res.latitude - 0.003299
  //       // console.log("定位成功", long);
  //       console.log("定位成功", res.latitude, res.longitude);
  //       var locationString = lat + "," + long;
  //       wx.request({
  //         url: 'https://apis.map.qq.com/ws/geocoder/v1/?l&get_poi=1',
  //         data: {
  //           "key": "YLFBZ-WHAWI-ZXUGH-53Q65-TOJ7E-ADBNQ",
  //           "location": locationString
  //         },
  //         method: 'GET',
  //         // header: {}, 
  //         success: function(res) {
  //           // success
  //           console.log("请求成功");
  //           console.log("请求数据:" + res.data.result.address);
  //         },
  //         fail: function() {
  //           // fail
  //           console.log("请求失败");
  //         },
  //         complete: function() {
  //           // complete
  //           console.log("请求完成");
  //         }
  //       })
  //     },
  //     fail: function() {
  //       // fail
  //       console.log("定位失败");
  //     },
  //     complete: function() {
  //       // complete
  //       console.log("定位完成");
  //     }
  //   })
  // }

})